from .base import BaseSpider
from bs4 import BeautifulSoup
import logging
from models.job import Job

class Spider58(BaseSpider):
    """58同城爬虫实现"""
    
    def __init__(self, config):
        self.config = config
        self.selectors = config['item_selectors']
    
    def parse_job_list(self, html):
        """解析职位列表"""
        soup = BeautifulSoup(html, 'lxml')
        jobs = []
        
        # 记录选择器信息
        logging.info(f"使用列表选择器: {self.config['list_selector']}")
        job_items = soup.select(self.config['list_selector'])
        logging.info(f"找到 {len(job_items)} 个职位项")
        
        for job_div in job_items:
            try:
                # 记录每个字段的提取过程
                title = job_div.select_one(self.selectors['title'])
                company = job_div.select_one(self.selectors['company'])
                salary = job_div.select_one(self.selectors['salary'])
                location = job_div.select_one(self.selectors['location'])
                description = job_div.select_one(self.selectors['description'])
                
                if not all([title, company, salary, location, description]):
                    logging.warning("部分字段未找到")
                    continue
                    
                job = Job(
                    title=title.text.strip(),
                    company=company.text.strip(),
                    salary=salary.text.strip(),
                    location=location.text.strip(),
                    description=description.text.strip()
                )
                
                logging.debug(f"解析到职位: {job.to_dict()}")
                jobs.append(job)
                
            except Exception as e:
                logging.error(f"解析职位失败: {str(e)}")
                continue
                
        return jobs
    
    def _parse_job_item(self, job_div):
        """解析单个职位信息"""
        return Job(
            title=job_div.select_one(self.selectors['title']).text.strip(),
            company=job_div.select_one(self.selectors['company']).text.strip(),
            salary=job_div.select_one(self.selectors['salary']).text.strip(),
            location=job_div.select_one(self.selectors['location']).text.strip(),
            description=job_div.select_one(self.selectors['description']).text.strip()
        )
        
    def extract_age_requirement(self, description):
        """从职位描述中提取年龄要求"""
        import re
        
        age_patterns = [
            r'(\d{2,3})[岁歲]以上',
            r'年龄(\d{2,3})以上',
            r'(\d{2,3})-\d{2,3}岁',
        ]
        
        for pattern in age_patterns:
            matches = re.findall(pattern, description)
            if matches:
                try:
                    age = int(matches[0])
                    return age
                except ValueError:
                    continue
        
        return None